package fr.inserm.umr915.vcfserver.bio.berkeley;

import java.lang.reflect.Constructor;
import java.math.BigInteger;

import com.sleepycat.bind.tuple.BigIntegerBinding;
import com.sleepycat.bind.tuple.BooleanBinding;
import com.sleepycat.bind.tuple.ByteBinding;
import com.sleepycat.bind.tuple.DoubleBinding;
import com.sleepycat.bind.tuple.FloatBinding;
import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.bind.tuple.LongBinding;
import com.sleepycat.bind.tuple.ShortBinding;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.bind.tuple.TupleBinding;


public class ColumnBinding
	{
	private int column=-1;
	private String name="";
	private String description=null;
	private Class<?> type=String.class;
	private TupleBinding<?> binding=new StringBinding();
	private Constructor<?> fromString;
	
	public ColumnBinding()
		{
		}
	
	public int getColumn()
		{
		return column;
		}

	public void setColumn(int column)
		{
		this.column = column;
		}

	public String getName()
		{
		return name;
		}

	public void setName(String name)
		{
		this.name = name;
		}

	public String getDescription()
		{
		return description==null?getName():description;
		}

	public void setDescription(String description)
		{
		this.description = description;
		}

	public Constructor<?> getFromString()
		{
		return fromString;
		}

	public void setFromString(Constructor<?> fromString)
		{
		this.fromString = fromString;
		}

	public Class<?> getType()
		{
		return type;
		}

	public void setBinding(TupleBinding<?> binding)
		{
		this.binding = binding;
		}

	public TupleBinding<?> getBinding()
		{
		return this.binding;
		}
	
	public void setType(Class<?> type)
		{
		this.type = type;
		if(type==null)
			{
			throw new NullPointerException();
			}
		else if(type==String.class)
			{
			this.binding=new StringBinding();
			}
		else if(type==Boolean.class)
			{
			this.binding=new BooleanBinding();
			}
		else if(type==Byte.class)
			{
			this.binding=new ByteBinding();
			}
		else if(type==Short.class)
			{
			this.binding=new ShortBinding();
			}
		else if(type==Integer.class)
			{
			this.binding=new IntegerBinding();
			}
		else if(type==Long.class)
			{
			this.binding=new LongBinding();
			}
		else if(type==BigInteger.class)
			{
			this.binding=new BigIntegerBinding();
			}
		else if(type==Float.class)
			{
			this.binding=new FloatBinding();
			}
		else if(type==Double.class)
			{
			this.binding=new DoubleBinding();
			}
		else
			{
			throw new IllegalArgumentException("bad class "+this.type);
			}
		}
	public Object cast(String s)
		{
		try
			{
			if(this.fromString==null)
				{
				this.fromString=this.type.getConstructor(String.class);
				}
			return this.fromString.newInstance(s);
			}
		catch (Exception e)
			{
			throw new RuntimeException(e);
			}
		}
	}
